import { UPDATE_USERINFO_TYPE, LOGOUE_USERINFO_TYPE } from './types';
import { commonLs } from '../../utils/common';

const reducer = (
  /* eslint-disable-next-line */// 免验证格式
  state = (() => {    // 自执行函数
    const [nameErr, adminname] = commonLs.get('adminname')
    const [tokenErr, token] = commonLs.get('token')
    const [checkedkeysErr, checkedkeys] = commonLs.get('checkedkeys')
    const [roleErr, role] = commonLs.get('role')
    return {
      adminname: nameErr ? '' : adminname,
      token: tokenErr ? '' : token,
      checkedkeys: checkedkeysErr ? [] : checkedkeys,
      role: roleErr ? 1 : role,
    }
  })(),
  action,
) => {
  const newState = { ...state }
  const { type, payload } = action

  switch (type) {
    case UPDATE_USERINFO_TYPE:
      return { ...newState, ...payload }
    case LOGOUE_USERINFO_TYPE:
      return { adminname: '', token: '' }
    default:
      break;
  }
  return newState
}
export default reducer
